package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.DTO.PoultryCategoryDownloadDTO;
import com.source.lightfowl.DTO.PoultryCategoryUploadDTO;
import com.source.lightfowl.VO.PoultryCategoryVO;
import com.source.lightfowl.basedata.dao.PoultryCategoryDao;
import com.source.lightfowl.basedata.dao.PoultryParentDao;
import com.source.lightfowl.basedata.dao.SlaughterHouseDao;
import com.source.lightfowl.basedata.entity.PoultryCategory;
import com.source.lightfowl.basedata.service.PoultryCategoryService;
import com.source.lightfowl.handler.MyException;
import com.source.lightfowl.person.entity.UsersExtends;
import com.source.lightfowl.utils.DateUtils;
import com.source.lightfowl.utils.StringTOArrayList;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 13:05
 */
@Service
public class PoultryCategoryServiceImpl implements PoultryCategoryService {

    @Autowired
    private PoultryCategoryDao poultryCategoryDao;

    @Autowired
    private PoultryParentDao parentDao;

    @Autowired
    private SlaughterHouseDao slaughterHouseDao;

    @Override
    public List<PoultryCategoryVO> findAll() {
        List<PoultryCategory> list = poultryCategoryDao.findAll();
        List<PoultryCategoryVO> collect = list.stream().map(s -> {
            PoultryCategoryVO vo = new PoultryCategoryVO();
            BeanUtils.copyProperties(s, vo);
            vo.setFatherName(parentDao.findNameById(s.getParentId()));
            vo.setHouseName(slaughterHouseDao.findNameById(Integer.valueOf(s.getSlaughterHouseId())));
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Page<PoultryCategory> findAllPage(int page, int pageNum) {
        return poultryCategoryDao.findAll(PageRequest.of(page-1,pageNum));
    }

    @Override
    public PoultryCategoryVO findOneById(String id) {
        PoultryCategoryVO vo = new PoultryCategoryVO();
        PoultryCategory s = poultryCategoryDao.findById(id);
        BeanUtils.copyProperties(s, vo);
        String pname = parentDao.findNameById(s.getParentId());
        String houseName = slaughterHouseDao.findNameById(Integer.valueOf(s.getSlaughterHouseId()));
        vo.setFatherName(pname);
        vo.setHouseName(houseName);
        return vo;
    }

    @Override
    public PoultryCategory findById(String id) {
        return poultryCategoryDao.findById(id);
    }

    @Override
    public PoultryCategory findByIdNumber(String id) {
        return poultryCategoryDao.findByIdNumber(id);
    }

    @Override
    public void save(PoultryCategory sh) {
        if (sh.getCreateDate() == null){
            sh.setCreateDate(LocalDateTime.now());
        }
        poultryCategoryDao.save(sh);
    }

    @Override
    public void deleteById(String id) {
        poultryCategoryDao.deleteById(id);
    }

    @Override
    public void deleteBatch(String ids) {
        ArrayList<Integer> list = StringTOArrayList.toArrayList(ids);
        poultryCategoryDao.deleteBatch(list);
    }

    @Override
    public Optional<PoultryCategory> findOptionalById(String id) {
        return poultryCategoryDao.findOptionalById(id);
    }

    @Override
    public void save(List<Object> list, HttpServletRequest request) {
        UsersExtends user = (UsersExtends) request.getSession().getAttribute("usersExtends");
        String loginName = user.getUser().getLoginName();
        List<PoultryCategory> poultryCategoryList = new ArrayList<>();
        for (Object object:
             list) {
            PoultryCategory category = new PoultryCategory();
            PoultryCategoryUploadDTO uploadDTO = (PoultryCategoryUploadDTO) object;
            if (uploadDTO.getNumber()==null){
                throw new MyException(500,"没有产品编号，无法录入");
            }
            category.setId(uploadDTO.getNumber());
            category.setNumber(uploadDTO.getNumber());
            category.setName(uploadDTO.getName());
            String parentId = parentDao.findIdByName(uploadDTO.getParentName());
            if(parentId==null){
                throw new MyException(500,"父级类别名称有错误！");
            }
            category.setParentId(parentId);
            Integer slaughterHouseId =  slaughterHouseDao.findIdByName(uploadDTO.getSlaughterHouseName());
            if (slaughterHouseId==null){
                throw new MyException(500,"市场名称有错误，未找到相应的市场");
            }
            category.setSlaughterHouseId(slaughterHouseId.toString());
            category.setCreateBy(loginName);
            category.setCreateDate(LocalDateTime.now());
            poultryCategoryList.add(category);
        }
        poultryCategoryDao.saveAll(poultryCategoryList);
    }

    @Override
    public List<Object> findAllDTO() {
        List<PoultryCategory> poultryCategoryList = poultryCategoryDao.findAll();
        List<Object> list = new ArrayList<>();
        for (PoultryCategory poultryCategory :
                poultryCategoryList) {
            PoultryCategoryDownloadDTO downloadDTO = new PoultryCategoryDownloadDTO();
            downloadDTO.setNumber(poultryCategory.getNumber());
            downloadDTO.setName(poultryCategory.getName());
            downloadDTO.setDetail(poultryCategory.getDetail());
            downloadDTO.setParentName(parentDao.findNameById(poultryCategory.getParentId()));
            downloadDTO.setSlaughterHouseId(poultryCategory.getSlaughterHouseId());
            downloadDTO.setSlaughterHouseName(slaughterHouseDao.findNameById(Integer.parseInt(poultryCategory.getSlaughterHouseId())));
            list.add(downloadDTO);
        }
        return list;
    }

}
